package com.way.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.AsyncQueryHandler;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.GroupMembership;
import android.provider.ContactsContract.Groups;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.way.bean.ContactBean;
import com.way.bean.GroupBean;
import com.way.dialer.R;
import com.way.uitl.BaseIntentUtil;
import com.way.uitl.SizeCallBackForMenu;
import com.way.view.MenuHorizontalScrollView;
import com.way.view.QuickAlphabeticBar;
import com.way.view.adapter.ContactHomeAdapter;
import com.way.view.adapter.MenuListAdapter;

public class HomeContactActivity extends Activity {

	private MenuHorizontalScrollView mScrollView;
	private ListView mMenuListView;
	private View mAcbuwaPage;
	private ImageView mMenuBtn;
	private ImageView mAddContactBtn;
	private MenuListAdapter mMenuListAdapter;
	private View[] mChildren;
	private LayoutInflater mLayoutInflater;

	private ContactHomeAdapter mContactHomeAdapter;
	private ListView mPersonListView;
	private List<ContactBean> mContactBeanLists;
	private AsyncQueryHandler mAsyncQueryHandler;
	private QuickAlphabeticBar mQuickAlphaBeticBar;

	private Map<Integer, ContactBean> mContactBeanIdMap = null;

	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		mLayoutInflater = LayoutInflater.from(this);
		setContentView(mLayoutInflater.inflate(R.layout.menu_scroll_view, null));

		mScrollView = (MenuHorizontalScrollView) findViewById(R.id.mScrollView);
		mMenuListAdapter = new MenuListAdapter(this, queryGroup());
		mMenuListView = (ListView) findViewById(R.id.menuList);
		mMenuListView.setAdapter(mMenuListAdapter);

		mAcbuwaPage = mLayoutInflater.inflate(R.layout.home_contact_page, null);
		mMenuBtn = (ImageView) this.mAcbuwaPage.findViewById(R.id.menuBtn);

		mPersonListView = (ListView) this.mAcbuwaPage
				.findViewById(R.id.acbuwa_list);

		mQuickAlphaBeticBar = (QuickAlphabeticBar) this.mAcbuwaPage
				.findViewById(R.id.fast_scroller);
		mAsyncQueryHandler = new MyAsyncQueryHandler(getContentResolver());
		init();

		mMenuBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				mScrollView.clickMenuBtn(HomeContactActivity.this);
			}
		});

		View leftView = new View(this);
		leftView.setBackgroundColor(Color.TRANSPARENT);
		mChildren = new View[] { leftView, mAcbuwaPage };
		mScrollView.initViews(mChildren,
				new SizeCallBackForMenu(this.mMenuBtn), this.mMenuListView);
		mScrollView.setMenuBtn(this.mMenuBtn);

		mAddContactBtn = (ImageView) findViewById(R.id.addContactBtn);
		mAddContactBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Uri insertUri = android.provider.ContactsContract.Contacts.CONTENT_URI;
				Intent intent = new Intent(Intent.ACTION_INSERT, insertUri);
				startActivityForResult(intent, 1008);
			}
		});

		startReceiver1();
	}

	private void init() {
		Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; // 联系人的Uri
		String[] projection = { ContactsContract.CommonDataKinds.Phone._ID,
				ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
				ContactsContract.CommonDataKinds.Phone.DATA1, "sort_key",
				ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
				ContactsContract.CommonDataKinds.Phone.PHOTO_ID,
				ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY }; // 查询的列
		mAsyncQueryHandler.startQuery(0, null, uri, projection, null, null,
				"sort_key COLLATE LOCALIZED asc"); // 按照sort_key升序查询
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (MenuHorizontalScrollView.menuOut == true)
				this.mScrollView.clickMenuBtn(HomeContactActivity.this);
			else
				this.finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	/**
	 * 数据库异步查询类AsyncQueryHandler
	 * 
	 * @author administrator
	 * 
	 */
	private class MyAsyncQueryHandler extends AsyncQueryHandler {

		public MyAsyncQueryHandler(ContentResolver cr) {
			super(cr);
		}

		/**
		 * 查询结束的回调函数
		 */
		@Override
		protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
			if (cursor != null && cursor.getCount() > 0) {

				mContactBeanIdMap = new HashMap<Integer, ContactBean>();

				mContactBeanLists = new ArrayList<ContactBean>();
				cursor.moveToFirst();
				for (int i = 0; i < cursor.getCount(); i++) {
					cursor.moveToPosition(i);
					String name = cursor.getString(1);
					String number = cursor.getString(2);
					String sortKey = cursor.getString(3);
					int contactId = cursor.getInt(4);
					Long photoId = cursor.getLong(5);
					String lookUpKey = cursor.getString(6);

					if (mContactBeanIdMap.containsKey(contactId)) {

					} else {

						ContactBean cb = new ContactBean();
						cb.setDisplayName(name);
						// if (number.startsWith("+86")) {//
						// 去除多余的中国地区号码标志，对这个程序没有影响。
						// cb.setPhoneNum(number.substring(3));
						// } else {
						cb.setPhoneNum(number);
						// }
						cb.setSortKey(sortKey);
						cb.setContactId(contactId);
						cb.setPhotoId(photoId);
						cb.setLookUpKey(lookUpKey);
						mContactBeanLists.add(cb);

						mContactBeanIdMap.put(contactId, cb);

					}
				}
				if (mContactBeanLists.size() > 0) {
					setAdapter(mContactBeanLists);
				}
			}
		}

	}

	private void setAdapter(List<ContactBean> list) {
		mContactHomeAdapter = new ContactHomeAdapter(this, list,
				mQuickAlphaBeticBar);
		mPersonListView.setAdapter(mContactHomeAdapter);
		mQuickAlphaBeticBar.init(HomeContactActivity.this);
		mQuickAlphaBeticBar.setListView(mPersonListView);
		mQuickAlphaBeticBar.setHight(mQuickAlphaBeticBar.getHeight());
		mQuickAlphaBeticBar.setVisibility(View.VISIBLE);
		mPersonListView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				ContactBean cb = (ContactBean) mContactHomeAdapter
						.getItem(position);
				showContactDialog(lianxiren1, cb, position);
			}
		});
	}

	private String[] lianxiren1 = new String[] { "拨打电话", "发送短信", "查看详细",
			"移动分组", "移出群组", "删除" };

	// 群组联系人弹出页
	private void showContactDialog(final String[] arg, final ContactBean cb,
			final int position) {
		new AlertDialog.Builder(this).setTitle(cb.getDisplayName())
				.setItems(arg, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {

						Uri uri = null;

						switch (which) {

						case 0:// 打电话
							String toPhone = cb.getPhoneNum();
							uri = Uri.parse("tel:" + toPhone);
							Intent it = new Intent(Intent.ACTION_CALL, uri);
							startActivity(it);
							break;

						case 1:// 发短息

							String threadId = getSMSThreadId(cb.getPhoneNum());

							Map<String, String> map = new HashMap<String, String>();
							map.put("phoneNumber", cb.getPhoneNum());
							map.put("threadId", threadId);
							BaseIntentUtil.intentSysDefault(
									HomeContactActivity.this,
									MessageBoxActivity.class, map);
							break;

						case 2:// 查看详细 修改联系人资料

							uri = ContactsContract.Contacts.CONTENT_URI;
							Uri personUri = ContentUris.withAppendedId(uri,
									cb.getContactId());
							Intent intent2 = new Intent();
							intent2.setAction(Intent.ACTION_VIEW);
							intent2.setData(personUri);
							startActivity(intent2);
							break;

						case 3:// 移动分组

							// Intent intent3 = null;
							// intent3 = new Intent();
							// intent3.setClass(ContactHome.this,
							// GroupChoose.class);
							// intent3.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
							// intent3.putExtra("联系人", contactsID);
							// Log.e("contactsID", "contactsID---"+contactsID);
							// ContactHome.this.startActivity(intent3);
							break;

						case 4:// 移出群组

							// moveOutGroup(getRaw_contact_id(contactsID),Integer.parseInt(qzID));
							break;

						case 5:// 删除

							showDelete(cb.getContactId(), position);
							break;
						}
					}
				}).show();
	}

	// 删除联系人方法
	private void showDelete(final int contactsID, final int position) {
		new AlertDialog.Builder(this)
				.setIcon(R.drawable.ic_launcher)
				.setTitle("是否删除此联系人")
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						// 源码删除
						Uri deleteUri = ContentUris.withAppendedId(
								ContactsContract.Contacts.CONTENT_URI,
								contactsID);
						Uri lookupUri = ContactsContract.Contacts.getLookupUri(
								HomeContactActivity.this.getContentResolver(),
								deleteUri);
						if (lookupUri != Uri.EMPTY) {
							HomeContactActivity.this.getContentResolver()
									.delete(deleteUri, null, null);
						}
						mContactHomeAdapter.remove(position);
						mContactHomeAdapter.notifyDataSetChanged();
						Toast.makeText(HomeContactActivity.this, "该联系人已经被删除.",
								Toast.LENGTH_SHORT).show();
					}
				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {

					}
				}).show();
	}

	/**
	 * 
	 * 查询所有群组 返回值List<ContactGroup>
	 */
	public List<GroupBean> queryGroup() {

		List<GroupBean> list = new ArrayList<GroupBean>();

		GroupBean cg_all = new GroupBean();
		cg_all.setId(0);
		cg_all.setName("全部");
		list.add(cg_all);

		Cursor cur = getContentResolver().query(Groups.CONTENT_URI, null, null,
				null, null);
		for (cur.moveToFirst(); !(cur.isAfterLast()); cur.moveToNext()) {
			if (null != cur.getString(cur.getColumnIndex(Groups.TITLE))
					&& (!"".equals(cur.getString(cur
							.getColumnIndex(Groups.TITLE))))) {
				GroupBean cg = new GroupBean();
				cg.setId(cur.getInt(cur.getColumnIndex(Groups._ID)));
				cg.setName(cur.getString(cur.getColumnIndex(Groups.TITLE)));
				list.add(cg);
			}
		}
		cur.close();
		return list;
	}

	private void queryGroupMember(GroupBean gb) {

		String[] RAW_PROJECTION = new String[] { ContactsContract.Data.RAW_CONTACT_ID };

		Cursor cur = getContentResolver().query(
				ContactsContract.Data.CONTENT_URI,
				RAW_PROJECTION,
				ContactsContract.Data.MIMETYPE + " = '"
						+ GroupMembership.CONTENT_ITEM_TYPE + "' AND "
						+ ContactsContract.Data.DATA1 + "=" + gb.getId(), null,
				"data1 asc");

		StringBuilder inSelectionBff = new StringBuilder()
				.append(ContactsContract.RawContacts._ID + " IN ( 0");
		while (cur.moveToNext()) {
			inSelectionBff.append(',').append(cur.getLong(0));
		}
		cur.close();
		inSelectionBff.append(')');

		Cursor contactIdCursor = getContentResolver().query(
				ContactsContract.RawContacts.CONTENT_URI,
				new String[] { ContactsContract.RawContacts.CONTACT_ID },
				inSelectionBff.toString(),
				null,
				ContactsContract.Contacts.DISPLAY_NAME
						+ "  COLLATE LOCALIZED asc ");
		Map<Integer, Integer> map = new HashMap<Integer, Integer>();
		while (contactIdCursor.moveToNext()) {
			map.put(contactIdCursor.getInt(0), 1);
		}
		contactIdCursor.close();

		Set<Integer> set = map.keySet();
		Iterator<Integer> iter = set.iterator();
		List<ContactBean> list = new ArrayList<ContactBean>();
		while (iter.hasNext()) {
			Integer key = iter.next();
			list.add(queryMemberOfGroup(key));
		}
		setAdapter(list);
	}

	private ContactBean queryMemberOfGroup(int id) {

		ContactBean cb = null;

		Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; // 联系人的Uri
		String[] projection = { ContactsContract.CommonDataKinds.Phone._ID,
				ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
				ContactsContract.CommonDataKinds.Phone.DATA1, "sort_key",
				ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
				ContactsContract.CommonDataKinds.Phone.PHOTO_ID,
				ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY }; // 查询的列
		Cursor cursor = getContentResolver().query(uri, projection,
				ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id,
				null, null);
		if (cursor != null && cursor.getCount() > 0) {
			mContactBeanLists = new ArrayList<ContactBean>();
			cursor.moveToFirst();
			for (int i = 0; i < cursor.getCount(); i++) {
				cursor.moveToPosition(i);
				String name = cursor.getString(1);
				String number = cursor.getString(2);
				String sortKey = cursor.getString(3);
				int contactId = cursor.getInt(4);
				Long photoId = cursor.getLong(5);
				String lookUpKey = cursor.getString(6);

				cb = new ContactBean();
				cb.setDisplayName(name);
				// if (number.startsWith("+86")) {// 去除多余的中国地区号码标志，对这个程序没有影响。
				// cb.setPhoneNum(number.substring(3));
				// } else {
				cb.setPhoneNum(number);
				// }
				cb.setSortKey(sortKey);
				cb.setContactId(contactId);
				cb.setPhotoId(photoId);
				cb.setLookUpKey(lookUpKey);
			}
		}
		cursor.close();
		return cb;
	}

	/**
	 * 数据库异步查询类AsyncQueryHandler
	 * 
	 * @author administrator
	 * 
	 */
	// private class GroupQueryHandler extends AsyncQueryHandler {
	//
	// public GroupQueryHandler(ContentResolver cr) {
	// super(cr);
	// }
	//
	// /**
	// * 查询结束的回调函数
	// */
	// protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
	// if (cursor != null && cursor.getCount() > 0) {
	// list = new ArrayList<ContactBean>();
	// cursor.moveToFirst();
	// for (int i = 0; i < cursor.getCount(); i++) {
	// ContentValues cv = new ContentValues();
	// cursor.moveToPosition(i);
	// String name = cursor.getString(1);
	// String number = cursor.getString(2);
	// String sortKey = cursor.getString(3);
	// int contactId = cursor.getInt(4);
	// Long photoId = cursor.getLong(5);
	// String lookUpKey = cursor.getString(6);
	//
	// ContactBean cb = new ContactBean();
	// cb.setDisplayName(name);
	// if (number.startsWith("+86")) {// 去除多余的中国地区号码标志，对这个程序没有影响。
	// cb.setPhoneNum(number.substring(3));
	// } else {
	// cb.setPhoneNum(number);
	// }
	// cb.setSortKey(sortKey);
	// cb.setContactId(contactId);
	// cb.setPhotoId(photoId);
	// cb.setLookUpKey(lookUpKey);
	// list.add(cb);
	// }
	// if (list.size() > 0) {
	// setAdapter(list);
	// }
	// }
	// }
	//
	// }

	public static String[] SMS_COLUMNS = new String[] { "thread_id" };

	private String getSMSThreadId(String adddress) {
		Cursor cursor = null;
		ContentResolver contentResolver = getContentResolver();
		cursor = contentResolver.query(Uri.parse("content://sms"), SMS_COLUMNS,
				" address like '%" + adddress + "%' ", null, null);
		String threadId = "";
		if (cursor == null || cursor.getCount() > 0) {
			cursor.moveToFirst();
			threadId = cursor.getString(0);
			cursor.close();
			return threadId;
		} else {
			cursor.close();
			return threadId;
		}
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (1008 == requestCode) {
			init();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	protected void onDestroy() {
		super.onDestroy();
		stopReceiver1();
	}

	private String ACTION1 = "SET_DEFAULT_SIG";
	private HomeContactActivity.BaseReceiver1 receiver1 = null;

	/**
	 * 打开接收器
	 */
	private void startReceiver1() {
		if (null == receiver1) {
			IntentFilter localIntentFilter = new IntentFilter(ACTION1);
			receiver1 = new HomeContactActivity.BaseReceiver1();
			this.registerReceiver(receiver1, localIntentFilter);
		}
	}

	/**
	 * 关闭接收器
	 */
	private void stopReceiver1() {
		if (null != receiver1)
			unregisterReceiver(receiver1);
	}

	public class BaseReceiver1 extends BroadcastReceiver {
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(ACTION1)) {
				mScrollView.clickMenuBtn(context);
				String str_bean = intent.getStringExtra("groupbean");
				Gson gson = new Gson();
				GroupBean gb = gson.fromJson(str_bean, GroupBean.class);
				if (gb.getId() == 0) {

					init();
				} else {

					queryGroupMember(gb);
				}
			}
		}
	}

}
